package com.sh.caryr.erp.util;

import com.baomidou.mybatisplus.service.IService;
import com.sh.caryr.erp.entity.basis.ExpressComp;
import com.sh.caryr.erp.exception.ResultCode;
import com.sh.caryr.erp.exception.RestServiceException;
import net.sf.json.JSONObject;
import org.apache.commons.collections.CollectionUtils;

import javax.servlet.http.HttpServletRequest;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.*;

public class UtilityTool {

    public static void printList(List list) {
        for (Object obj : list) {
            System.out.println(JSONObject.fromObject(obj));
        }
    }

    public static String firstLowerCase(String str) {
        char[] chars = str.toCharArray();
        chars[0] += 32;
        return String.valueOf(chars);
    }

    public static String getRequestExcludeDomain(String url) {
        if (url == null) return null;
        String page = "";
        int index = url.indexOf(ERPConstants.PROJECT_NAME + "/");
        if (index != -1) {
            page = url.substring(index + (ERPConstants.PROJECT_NAME + "/").length());
        }
        return page;
    }

    @Deprecated
    public static String getRequestPage(String url) {
        String page = "";
        int index = url.indexOf(ERPConstants.ROUTE_MATERIAL + "/");
        if (index != -1) {
            page = url.substring(index + (ERPConstants.ROUTE_MATERIAL + "/").length());
        }
        return page;
    }

    public static String getRequestPageNew(String currentURL) {
        String prefix = ERPConstants.PROJECT_NAME;
        int index = currentURL.indexOf(prefix + "/");
        if (index != -1) {
            currentURL = currentURL.substring(index + (prefix + "/").length());
        }
        return currentURL;
    }


    public static List<Long> convertStringArray2LongList(String[] intStr) {
        List<Long> ints = new ArrayList<>();
        if (intStr == null || intStr.length == 0) {
            return ints;
        }
        try {
            for (String s : intStr) {
                ints.add(Long.valueOf(s));
            }
            return ints;
        } catch (Exception e) {
            e.printStackTrace();
            return new ArrayList<>();
        }
    }

    public static Set<Integer> convertStringArray2Set(String[] intStr) {
        Set<Integer> indexs = new HashSet<>();
        if (intStr == null || intStr.length == 0) {
            return indexs;
        }
        try {
            for (int i = 0; i < intStr.length; i++) {
                indexs.add(Integer.valueOf(intStr[i]));
            }
            return indexs;
        } catch (Exception e) {
            e.printStackTrace();
            return new HashSet<>();
        }
    }

    /**
     * 软删除，isActive = 0
     *
     * @param list
     */
    public static void softDelete(Class clz, List<Long> list, IService service) throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {
        List<Object> objects = service.selectBatchIds(list);
        Method setIsActive = clz.getMethod("setIsActive", int.class);
        for (Object object : objects) {
            setIsActive.invoke(object, ERPConstants.ENITYT_INACTIVE);
        }
        if (!service.updateBatchById(objects)) {
            throw new RestServiceException(ResultCode.UPDATE_FAILED);
        }
    }

    public static List insertUpdateDeleteEntities(Class clz, List oldEntities, List newEntities, IService service) throws Exception {
        Method getIdMethod = clz.getDeclaredMethod("getId");
        List<Object> deleteEntities = new ArrayList();
        List<Long> deleteIds = new ArrayList();
        for (Object oldObj : oldEntities) {
            if (!MyBatisUtil.findEntity(oldObj, newEntities, getIdMethod)) {
                deleteEntities.add(oldObj);
                deleteIds.add((Long) getIdMethod.invoke(oldObj));
            }
        }
        if (deleteIds.size() != 0) {
            service.deleteBatchIds(deleteIds);
        }
        if (CollectionUtils.isNotEmpty(newEntities)) {
            service.insertOrUpdateBatch(newEntities);
        }
        return deleteEntities;
    }

    public static List<ExpressComp> getExpressComps() {
        List<ExpressComp> expressComps = new ArrayList<>();
        expressComps.add(new ExpressComp("顺丰快递", "shunfeng"));
        expressComps.add(new ExpressComp("邮政EMS", "ems"));
        expressComps.add(new ExpressComp("圆通快递", "yuantong"));
        expressComps.add(new ExpressComp("申通快递", "shentong"));
        expressComps.add(new ExpressComp("韵达快递", "yunda"));
        expressComps.add(new ExpressComp("中通快递", "zhongtong"));
        expressComps.add(new ExpressComp("宅急送", "zhaijisong"));
        expressComps.add(new ExpressComp("天天快递", "tiantian"));
        expressComps.add(new ExpressComp("百世汇通", "baishi"));
        expressComps.add(new ExpressComp("德邦", "debang"));
        expressComps.add(new ExpressComp("华宇", "huayu"));
        expressComps.add(new ExpressComp("京东快递", "jingdong"));
        return expressComps;
    }

    public static String getExpCompCodeByName(List<ExpressComp> expComps, String expCompName) {
        if (expCompName == null || expComps == null) {
            return "";
        }
        for (ExpressComp expComp : expComps) {
            if (expCompName.equals(expComp.getName())) {
                return expComp.getCode();
            }
        }
        return "";
    }

    public static String getJSONValueSafe(JSONObject obj, String key) {
        return obj.containsKey(key) ? obj.getString(key) : "";
    }

}
